<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px"
      @submit.native.prevent>
      <el-form-item label="字典标签" prop="dictKey">
        <el-input v-model="queryParams.domain.dictKey" placeholder="请输入字典标签" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="字典值" prop="dictValue">
        <el-input v-model="queryParams.domain.dictValue" placeholder="请输入字典值" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
        <el-button type="primary" plain icon="el-icon-delete" size="mini" @click="handleDelete">删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>


    <el-table v-loading="loading" :data="dataList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50" align="center" />
      <el-table-column label="字典标签" align="center" prop="dictKey" />
      <el-table-column label="字典值" align="center" prop="dictValue" />
      <el-table-column label="创建人" align="center" prop="createBy" />
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpd(scope.row)">修改</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="字典标签" prop="dictKey">
          <el-input v-model="form.dictKey" placeholder="填写字典标签" />
        </el-form-item>
        <el-form-item label="字典值" prop="dictValue">
          <el-input v-model="form.dictValue" placeholder="填写字典值" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listData,
    addData,
    removeData,
    updateData
  } from "@/api/system/dictValue";

  export default {
    name: "Post",
    dicts: ['sys_normal_disable'],
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 岗位表格数据
        dataList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        typeId:0,
        // 查询参数
        queryParams: {
          domain: {
            typeId: ""
          },
          pageNum: 1,
          pageSize: 10,
          sortName: "",
          sortOrder: ""
        },
        // 表单参数
        form: {typeId:0},
        // 表单校验
        rules: {
          dictKey: [{
            required: true,
            message: "字典标签不能为空",
            trigger: "blur"
          }],
          dictValue: [{
            required: true,
            message: "字典名称不能为空",
            trigger: "blur"
          }]
        },

      };
    },
    created() {
      const typeId = this.$route.query.typeId;
      this.queryParams.domain.typeId = typeId;
      this.typeId = typeId;
      this.form.typeId = typeId;
      this.getList();
    },
    methods: {
      /** 查询岗位列表 */
      getList() {
        this.loading = true;
        listData(this.queryParams).then(response => {
          this.dataList = response.data.records;
          this.total = response.data.total;
          this.loading = false;
        });
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.dictId)
        this.single = selection.length != 1
        this.multiple = !selection.length
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {typeId:this.typeId}
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title="新增字典值"
      },
      /** 修改按钮操作 */
      handleUpd(row) {
        this.reset();
        this.form = JSON.parse(JSON.stringify(row));
        this.open = true;
        this.title="修改字典值";
      },
      /** 提交按钮 */
      submitForm: function() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if(this.form.dictId == undefined){
              addData(this.form).then(response => {
                this.$modal.msgSuccess("添加成功");
                this.open = false;
                this.getList();
              });
            }else{
              updateData(this.form).then(response => {
                this.$modal.msgSuccess("修改");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete() {
        if(this.ids.length == 0){
            this.msgError("请选择一条数据");
            return;
        }
        const removeIds = this.ids;
        this.$confirm('是否确认删除数据？').then(function() {
          return removeData(removeIds);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        });
      },
     }
  };
</script>
